package com.tentcoo.zhaogonggu.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.tentcoo.zhaogonggu.R;

/**
 * Created by Gunter on 2015/4/18.
 * @author 林桂填
 */
public class RankView extends View {


    private final int POSITION_LEFT = 0;
    private final int POSITION_RIGHT = 1;
    private int rank = 4; //等级级数，初始化为0级
    private Bitmap rankIcon; //等级图标
    private Rect iconRect; //绘图矩形域，这里需要用到的绘图矩形是正方形
    private int rectSize; //这里我们绘图矩形域的边长
    private int iconSpacing; //图标间距
    private int positionInParent;


    //第一个构造方法调用第二个，第二个调用第三个，这样无论外部实例化多少个参数都得调用第三个构造方法
    public RankView(Context context) {
        this(context, null);
    }

    public RankView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RankView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //从属性文件attrs.xml中获取图片资源，并转换成BitMap
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RankView);
        int count = typedArray.getIndexCount();
        for(int i=0;i<count;i++) {
            switch (typedArray.getIndex(i)) {
                case R.styleable.RankView_rank_icon:
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) typedArray.getDrawable(i);
                    rankIcon = bitmapDrawable.getBitmap();
                    break;
                case R.styleable.RankView_rank_position_in_parent:
                    positionInParent = typedArray.getInt(i,POSITION_LEFT);
            }
        }
        typedArray.recycle();
        iconRect = new Rect();

        iconSpacing = 20;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        rectSize = getMeasuredHeight(); //把该View的高度设置为绘图矩形的边长，该就是布局文件中设置好的20dp
        getLayoutParams().width = (rectSize+iconSpacing)*rank-iconSpacing;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (rank != 0) {
            if(positionInParent == POSITION_LEFT){
                int left = 0;
                for (int i = 0; i < rank; i++) {
                    iconRect.set(left, 0, left + rectSize, rectSize);
                    canvas.drawBitmap(rankIcon, null, iconRect, null);
                    left = left + rectSize + iconSpacing;
                }
            }else{
                int left = getMeasuredWidth();
                for (int i = 0; i < rank; i++) {
                    iconRect.set(left - rectSize, 0,left , rectSize);
                    canvas.drawBitmap(rankIcon, null, iconRect, null);
                    left = left - rectSize - iconSpacing;
                }

            }
        }
    }

    //提供给外部调用，进行等级设置，并进行重绘等级图标
    public void setRank(int rank) {
        this.rank = rank;
        invalidateView();
    }

    private void invalidateView() {
        //判断UI线程是否等于当前线程
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else
            //UI主线程则需要调用该方法进行重绘
            postInvalidate();
    }
}
